<?php

//function getInstanstList($rowList, $obj){
//    $rst = array();
//    $class= get_class($obj);
//    foreach($rowList as $row){
//        array_push($rst, new $class($row));
//    }
//
//    return $rst;
//}

function iterTrimStr(&$param){
    foreach($param as $key => $val){
        if(is_string($val)){
            $param[$key] = trim($val);
        } else if(is_array($val)){
            iterTrimStr($param[$key]);
        }
    }
}

function registerAclCfg(){
    require_once 'Zend/Config/Yaml.php';
    require_once 'Zend/Registry.php';
    $aclCfg = new Zend_Config_Yaml(
        APPLICATION_PATH . '/configs/acl_config.yaml'
    );
    Zend_Registry::set('aclCfg', $aclCfg);
//    echo $aclCfg->aclList->home->findUserMenuTree;
}


function registerExpResCfg(){
    require_once 'Zend/Config/Yaml.php';
    require_once 'Zend/Registry.php';
    $errorResources = new Zend_Config_Yaml(
        APPLICATION_PATH . '/configs/errorResources.yaml'
    );
    Zend_Registry::set('errorResources', $errorResources);
//    echo resStringReplace($errorResources->get('404'), array("ss", "1122"));
}

function resStringReplace($tpl, $paramMap){
    $paramKeyList = array();
    $paramValueList = array();

    foreach($paramMap as $paramKey => $paramValue){
        array_push($paramKeyList, "{" . $paramKey . "}");
        array_push($paramValueList, $paramValue);
    }

    $rst = str_replace($paramKeyList, $paramValueList, $tpl);
    return $rst;
}

function registZendLog()
{
    require_once 'Zend/Log/Writer/Stream.php';
    require_once 'Zend/Log.php';
    require_once 'Zend/Registry.php';
    require_once 'Zend/Log/Formatter/Simple.php';

    $logPath = APPLICATION_PATH . '/../logs/';

    if (!is_dir($logPath)) {
        try {
            mkdir($logPath, 0700);
        } catch (Exception $e) {
            echo nl2br($e->__toString());
        }
    }

    $writer = new Zend_Log_Writer_Stream($logPath . date('Ymd') . '.log');
//    $format = '%timestamp% %priorityName% (%priority%): %message% in file %file% on line %line%' . PHP_EOL;
//    $formatter = new Zend_Log_Formatter_Simple($format);
//    $writer->setFormatter($formatter);
    $log = new Zend_Log($writer);

    if(APPLICATION_ENV != "production"){
        require_once "Zend/Log/Writer/Firebug.php";
        $writer = new Zend_Log_Writer_Firebug();
        $log->addWriter($writer);
    }

    Zend_Registry::set('log', $log);
    $log->addFilter(getZendLogPirorFromEnv());
}

function getZendLogPirorFromEnv()
{
    $priority = Zend_Log::DEBUG;

    switch (getenv('ZEND_LOG_PRIOR')) {
        case 'EMERG':
            $priority = Zend_Log::EMERG;
            break;
        case 'ALERT':
            $priority = Zend_Log::ALERT;
            break;
        case 'CRIT':
            $priority = Zend_Log::CRIT;
            break;
        case 'ERR':
            $priority = Zend_Log::ERR;
            break;
        case 'WARN':
            $priority = Zend_Log::WARN;
            break;
        case 'NOTICE':
            $priority = Zend_Log::NOTICE;
            break;
        case 'INFO':
            $priority = Zend_Log::INFO;
            break;
        case 'DEBUG':
            $priority = Zend_Log::DEBUG;
            break;
    }

    return $priority;
}

function jwawaErrorLoghandler($errno, $errstr, $errfile, $errline, $errcontext)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    require_once 'Zend/Log.php';
    require_once 'Zend/Registry.php';

    $log = Zend_Registry::get('log');
    $log->setEventItem('file', $errfile);
    $log->setEventItem('line', strval($errline));
    $priority = Zend_Log::NOTICE;

    switch ($errno) {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
        case E_PARSE:
            $priority = Zend_Log::ERR;
            break;
        case E_WARNING:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_USER_WARNING:
            $priority = Zend_Log::WARN;
            break;
        case E_NOTICE:
        case E_USER_NOTICE:
        case E_STRICT:
        case E_DEPRECATED:
        default:
            $priority = Zend_Log::NOTICE;
    }

    $log->log($errstr, $priority);
    $e = new Exception();
    $log->log('PHP Stack trace:' . PHP_EOL . $e->getTraceAsString(), $priority);

    /* Don't execute PHP internal error handler */
    return true;
}
